package Action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Beans.Agent;
import Beans.Branch;
import Beans.BranchSMSTemplate;
import Beans.Terminal;
import DAO.AgentDao;
import DAO.BoxDao;
import DAO.BranchDao;
import DAO.BranchSMSTemplateDao;
import DAO.TerminalDao;

import Utils.GetHelper;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BranchAction extends ActionSupport {

	private int branchID;
	private int agentID;
	private int estateID;
	private String branchCity;
	private String branchProvince;
	private String branchName;
	private String smsTemplate;
	private List<Branch> branchs;
	private String returnType;
	private final String urlPath = "http://112.124.59.126:8080/YouBoxApi/controller?";
	Map<String, Object> message;

	private Branch branch = new Branch();
	private Agent agent = new Agent();
	private BranchSMSTemplate template = new BranchSMSTemplate();
	
	List<Agent> agents;
	List<BranchSMSTemplate> templates;

	BranchDao branchDao = new BranchDao();
	TerminalDao terminalDao = new TerminalDao();
	AgentDao agentDao = new AgentDao();
	BranchSMSTemplateDao templateDao = new BranchSMSTemplateDao();
	BoxDao boxDao = new BoxDao();

	/**
	 * 通过城市获取代理商信息
	 * 
	 * @return
	 */
	public String getAgentByCity() {
		agents = agentDao.getAgentByCity(branchCity);
		return "jsonSuccess";
	}

	/**
	 * 获取网点信息
	 * 
	 * @return
	 */
	public String getBranchById() {
		branch = branchDao.getBranchById(branchID);
		branchProvince = branch.getBranchProvince();
		branchCity = branch.getBranchCity();
		templates = templateDao.getByBranchId(branchID);
		if(templates.size()==1){
			template = templates.get(0);
		}
		return "success";
	}
	
	public String getByBranchID() {
		branch = branchDao.getBranchById(branchID);
		branchs = new ArrayList<Branch>();
		branchs.add(branch);
		return "getBranch";
	}

	/**
	 * 获取网点与代理商信息
	 * 
	 * @return
	 */
	public String getBranchAgentList() {
		branch = branchDao.getBranchById(branchID);
		agent = agentDao.getAgentById(agentID);
		templates = templateDao.getByBranchId(branchID);
		if(templates.size()==1){
			template = templates.get(0);
		}
		return "success";
	}
	/**
	 * 选择代理商网点
	 * @return
	 */
	public String selectAgentBranch() {
		branchs = branchDao.getBranchByAgentId(agentID);
		return "selectBranch";
	}

	/**
	 * 编辑网点
	 * 
	 * @return
	 */
	public String editBranch() {
		List<Terminal> terminals = terminalDao.returnTermialByBranchID(branchID);
		branch.setBranchId(branchID);
		if (agentID == 0) {
			branch.setAgent(agentDao.getAgentById(1));
		} else {
			branch.setAgent(agentDao.getAgentById(agentID));
		}
		branch.setBranchCity(branchCity);
		branch.setBranchProvince(branchProvince);
		branchDao.edit(branch);
		templates = templateDao.getByBranchId(branchID);
		if(templates.size()==1){
			template = templates.get(0);
			template.setSmsTemplate(smsTemplate);
			templateDao.edit(template);
		}else if(templates.size()==0){
			template.setBranchId(branch.getBranchId()+"");
			template.setSmsTemplate(smsTemplate);
			templateDao.save(template);
		}
		for(Terminal t:terminals){
			GetHelper.doHttpGet(urlPath+"funcID=0029&terminalId="+t.getTerminalId());
		}
		return "success";
	}

	/**
	 * 删除网点
	 * 
	 * @return
	 */
	public String deleteBranch() {
		boolean canDelete = true;
		branch = branchDao.getBranchById(branchID);
		ArrayList<Terminal> terminals = terminalDao.returnTermialByBranchID(branchID);
		for(Terminal t:terminals){
			if(boxDao.returnBoxesInThisTerminal(t.getTerminalId())>0){
				canDelete=false;
				break;
			}
		}
		if (canDelete){
			branch.setStatus(1);
			branchDao.edit(branch);
			for(Terminal t:terminals){
				t.setStatus(2);
				terminalDao.edit(t);
			}
		}
		return "success";
	}

	/**
	 * 新增网点
	 * 
	 * @return
	 */
	public String registerBranch() {
		if (agentID == 0) {
			branch.setAgent(agentDao.getAgentById(1));
		} else {
			branch.setAgent(agentDao.getAgentById(agentID));
		}
		branch.setBranchCity(branchCity);
		branch.setBranchProvince(branchProvince);
		branch=branchDao.save(branch);
		template.setBranchId(branch.getBranchId()+"");
		template.setSmsTemplate(smsTemplate);
		templateDao.save(template);
		return "success";
	}

	/**
	 * 获取所有网点
	 * 
	 * @return
	 */
	public String getAllBranch() {
		agents = agentDao.getAllAgent();
		branchs = branchDao.getAll();
		if (returnType == null) {

			return "success";
		} else if (returnType.equals("json")) {
			message = new HashMap<String, Object>();
			message.put("result", 0);
			message.put("branchs", branchs);
			return "jsonSuccess";
		}
		return "success";
	}
	/**
	 * 根据物业公司获取设备网点
	 * @return
	 */
	public String getBranchByEstateID() {
		branchs = branchDao.getBranchByEstateID(estateID);
		return "getBranch";
	}

	/**
	 * 获取所有网点
	 * 
	 * @return
	 */
	public String getAllBranchOfpack() {
		branchs = branchDao.getAll();
		return "selectBranch";
	}

	/**
	 * 获取所有删选网点
	 * 
	 * @return
	 */
	public String getAllSearchBranch() {
		branchs = branchDao.getAllSearchBranch(branchProvince, branchCity,
				agentID, branchName);
		agents = agentDao.getAllAgent();
		if (returnType == null) {
			return "success";
		} else if (returnType.equals("json")) {
			message = new HashMap<String, Object>();
			message.put("result", 0);
			message.put("branchs", branchs);
			return "jsonSuccess";
		}
		return "success";
	}

	public List<Branch> getBranchs() {
		return branchs;
	}

	public void setBranchs(List<Branch> branchs) {
		this.branchs = branchs;
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}

	public Map<String, Object> getMessage() {
		return message;
	}

	public void setMessage(Map<String, Object> message) {
		this.message = message;
	}

	public int getBranchID() {
		return branchID;
	}

	public void setBranchID(int branchID) {
		this.branchID = branchID;
	}

	public Branch getBranch() {
		return branch;
	}

	public void setBranch(Branch branch) {
		this.branch = branch;
	}

	public int getAgentID() {
		return agentID;
	}

	public void setAgentID(int agentID) {
		this.agentID = agentID;
	}

	public List<Agent> getAgents() {
		return agents;
	}

	public void setAgents(List<Agent> agents) {
		this.agents = agents;
	}

	public String getBranchCity() {
		return branchCity;
	}

	public void setBranchCity(String branchCity) {
		this.branchCity = branchCity;
	}

	public String getBranchProvince() {
		return branchProvince;
	}

	public void setBranchProvince(String branchProvince) {
		this.branchProvince = branchProvince;
	}

	public Agent getAgent() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	public String getBranchName() {
		return branchName;
	}

	public void setBranchName(String branchName) {
		this.branchName = branchName;
	}

	public String getSmsTemplate() {
		return smsTemplate;
	}

	public void setSmsTemplate(String smsTemplate) {
		this.smsTemplate = smsTemplate;
	}

	public BranchSMSTemplate getTemplate() {
		return template;
	}

	public void setTemplate(BranchSMSTemplate template) {
		this.template = template;
	}

	public List<BranchSMSTemplate> getTemplates() {
		return templates;
	}

	public void setTemplates(List<BranchSMSTemplate> templates) {
		this.templates = templates;
	}

	public int getEstateID() {
		return estateID;
	}

	public void setEstateID(int estateID) {
		this.estateID = estateID;
	}
	
}
